package flychess.view;

import flychess.data.GameMap;
import flychess.entity.Player;
import flychess.util.XMLParser;
import javax.swing.*;
import java.awt.*;

public class DisplayMapInWindow extends DisplayMap{
    private final int rowLen;
    private final int colLen;
    private final int colNum;
    AppFrame frame = new AppFrame();
    public DisplayMapInWindow(GameMap map) {
        frame.label.setText("图例说明："+icons.get("start")+" 起点; "+icons.get("end")+" 终点; "+
                icons.get("pause")+" 暂停; "+ icons.get("luckyTurn")+" 幸运轮盘; "+
                icons.get("landMine")+" 地雷; "+icons.get("timeTunnel")+" 时空隧道; "+
                icons.get("normal")+" 普通");
        this.gMap = map;
        this.colLen = XMLParser.getMapViewConfig("colLen");
        colNum = XMLParser.getMapViewConfig("colNum");
        this.rowLen = (gMap.mapLen - colLen * colNum) / (colNum + 1) + 1;
    }
    @Override
    public void showMap(Player player1, Player player2) {
        StringBuilder buffer = new StringBuilder();
        int pos = 0;
        for (int i = 1; i <= colNum; i++) {
            if (i % 2 == 0) {
                showLine2(pos, pos += rowLen, player1, player2, buffer);     //显示地图偶数行
                showLLine(pos, pos += colLen, player1, player2, buffer);     //显示地图左列
            }
            else {
                showLine1(pos, pos += rowLen, player1, player2, buffer);   //显示地图奇数行
                showRLine(pos, pos += colLen, player1, player2, buffer);     //显示地图右列
            }
        }
        if (pos < gMap.mapLen && colNum % 2 != 0) {
            for (int j = 0; j < rowLen - 1; j++) System.out.print("　");
        }
        showLine1(pos, this.gMap.mapLen, player1, player2, buffer);     //显示地图第3行
        newLine(buffer);
        frame.out.setText(buffer.toString());
    }

    public void showLine1(int start, int end, Player player1, Player player2, StringBuilder buffer){
        for(int i = start; i < end; i++){
            buffer.append(getGraph(gMap.map[i], i, player1, player2));
        }
        newLine(buffer);                        //换行
    }
    public void showLine2(int start, int end, Player player1, Player player2, StringBuilder buffer){
        for(int i = end - 1; i >= start; i--){
            buffer.append(getGraph(gMap.map[i], i, player1, player2));
        }
        newLine(buffer);                        //换行
    }
    public void showRLine(int start, int end, Player player1, Player player2, StringBuilder buffer){
        for (int i = start;  i < end; i++) {
            for (int j = 0; j < rowLen - 1; j++) buffer.append("　");
            buffer.append(getGraph(gMap.map[i], i, player1, player2));
            newLine(buffer);
        }
    }
    public void showLLine(int start, int end, Player player1, Player player2, StringBuilder buffer){
        for(int i = start; i < end; i++){
            buffer.append(getGraph(gMap.map[i], i, player1, player2));
            newLine(buffer);
        }
    }

    @Override
    public void showPlayerIcon(Player player1, Player player2) {
        StringBuilder str = new StringBuilder();
        outPlayerIcon(player1, str);
        str.append("      ");
        outPlayerIcon(player2, str);
        frame.label2.setText(str.toString());
    }

    /**
     * 输出换行到StringBuilder中
     * @param stringBuilder StringBuilder对象
     */
    private void newLine(StringBuilder stringBuilder) {
        stringBuilder.append("\n");
    }
    private void outPlayerIcon(Player player, StringBuilder buffer) {
        buffer.append(player.getPlayerID());
        buffer.append("的士兵(");
        buffer.append(player.getPlayerName());
        buffer.append(")：");
        buffer.append(player.getPlayerSemiIcon());
    }

    @Override
    public void printIconInfo() {

    }
}
class AppFrame extends JFrame {
    JLabel label = new JLabel("图例说明");// 游戏的图例说明标签
    JTextArea out = new JTextArea(40, 70);//表示输出标签
    JLabel label2 = new JLabel(); // 玩家的图标说明
    public  AppFrame(){
        this.setTitle("飞行棋游戏地图");
        setLayout(new FlowLayout());//设定布局为流式布局（加的东西一个个放，放不下就放下一排）
        out.setFont(new Font("黑体", Font.BOLD,18));
        out.setForeground(new Color(44, 162, 117));
        label.setFont(new Font("楷体", Font.BOLD,20));
        label.setForeground(new Color(204, 113, 63));
        label2.setFont(new Font("楷体", Font.BOLD,24));
        label2.setForeground(new Color(218, 64, 45));
        getContentPane().add(label2);
        getContentPane().add(label);//内容面板上面增加标签
        getContentPane().add(out);
        setBounds(50, 50, 900, 600);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);//显示
    }
}
